package m202406.d11;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/11 21:01
 */
public class Q226 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        treeNode.left = new TreeNode(2);
//        treeNode.right = new TreeNode(3);
        treeNode.left.left = new TreeNode(4);
        treeNode.left.right = new TreeNode(5);
//        treeNode.right.left = new TreeNode(6);
//        treeNode.right.right = new TreeNode(7);
        Q226 q226 = new Q226();
        q226.invertTree(treeNode);
    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            TreeNode temp = pop.left;
            pop.left = pop.right;
            pop.right = temp;
            if (pop.left != null) {
                stack.push(pop.left);
            }
            if (pop.right != null) {
                stack.push(pop.right);
            }
        }
        return root;
    }

    public TreeNode invertTree1(TreeNode root) {
        if (root == null) {
            return null;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        LinkedList<LinkedList<TreeNode>> lines = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            LinkedList<TreeNode> line = new LinkedList<>();
            boolean flag = false;
            for (TreeNode treeNode : queue) {
                if (treeNode != null) {
                    flag = true;
                    line.addLast(treeNode.left);
                    line.addLast(treeNode.right);
                } else {
                    line.addLast(null);
                    line.addLast(null);
                }
            }
            if (!flag) {
                break;
            }
            lines.add(queue);
            queue = line;
        }

        for (LinkedList<TreeNode> line : lines) {
            for (int i = 0, j = line.size() - 1; i < j; i++, j--) {
                Collections.swap(line, i, j);
            }
        }


        for (int i = 1; i < lines.size(); i++) {
            LinkedList<TreeNode> parents = lines.get(i - 1);
            LinkedList<TreeNode> children = lines.get(i);
            for (int j = 0; j < parents.size(); j++) {
                TreeNode p = parents.get(j);
                if (p == null) {
                    continue;
                }
                parents.get(j).left = children.get(2 * j);
                parents.get(j).right = children.get(2 * j + 1);
            }
        }
        return lines.get(0).get(0);
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
